package com.wwwc.index.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.ejb.EJBManager;
import com.wwwc.index.ejb.forum.*;

public class ForumDetailsDisplayServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	try {
	    response.setContentType("text/html");
	    PrintWriter out        = response.getWriter();
	    HttpSession session    = request.getSession(true);
	    
	    String call_path       = request.getContextPath()+request.getServletPath();
	    String userIp          = request.getRemoteAddr();
	    String lid = (String)session.getAttribute("lid");

	    if(lid == null) {
		lid = "en";
	    }

	    ResourceBundle message = (ResourceBundle)session.getAttribute("ForumMessage_"+lid);
	    
	    if (message == null) {
		try{
		    message = ResourceBundle.getBundle("Forum_"+lid);
		    session.setAttribute("ForumMessage_"+lid, message);
		}
		catch(Exception e) {
		    System.out.println(e);
		    return;
		}
	    }

	    String page_id     = request.getParameter("pid");
	    if(page_id == null) {
		page_id = "1";
	    }

	    UserProfile user_profile = (UserProfile)session.getAttribute("UserProfile");
	    DirectoryDetails dd = (DirectoryDetails)session.getAttribute("LastDirectoryDetails_"+lid);

	    if(dd == null) {
		System.out.println("ForumDetailDisplayServlet:Error:101");
		return;
	    }

	    String managers     = dd.getManagers();
	    String admins       = dd.getAdmins();
	    boolean isAdmin     = ForumUtils.isUserAdmin(admins, user_profile);
	    boolean isManager   = ForumUtils.isUserManager(managers, user_profile);
	    String isSearch     = request.getParameter("isSearch");

	    ForumDetails fd = (ForumDetails)request.getAttribute("ParentForumDetails");

	    //======================================
	    // manager and admin bar
	    //======================================
	    StringBuffer msbf = new StringBuffer();
	    msbf.append("\n<TABLE border='0' align='center'>");
	    msbf.append("\n<TR>");
	    msbf.append(MyHtml.makeLinkCell("Print", "'/index/Print' target='_blank'"));
	    msbf.append(MyHtml.makeLinkCell("Back", "javaScript:history.go(-1);"));

	    if(isSearch == null) {
		if(isManager) {
		    msbf.append(MyHtml.makeLinkCell("Modify", call_path+"?fid="+fd.getFileId()+"&pid="+page_id+"&aid=8-0"));
		    msbf.append(MyHtml.makeLinkCell("Remove", call_path+"?fid="+fd.getFileId()+"&pid="+page_id+"&aid=8-1"));
		}

		if(isAdmin) {
		    msbf.append(MyHtml.makeLinkCell("Edit", call_path+"?fid="+fd.getFileId()+"&pid="+page_id+"&aid=9-0"));
		    msbf.append(MyHtml.makeLinkCell("Delete", call_path+"?fid="+fd.getFileId()+"&pid="+page_id+"&aid=9-1"));
		}
	    }

	    msbf.append("\n</TR></TABLE>");
  	    
	    //=================================================================================================
	    // document detail
	    //=================================================================================================
	    out.println("\n<TABLE width='100%' border=0 cellSpacing='0' cellPadding='0'>");
	    out.println("\n<TR><TD align='center' style='BORDER-TOP: 1px solid; BORDER-LEFT: 1px solid;BORDER-RIGHT: 1px solid;'>");
	    out.println("<BR><B>"+fd.getTitle()+"</B></TD></TR>");
	    out.println("\n<TR><TD align='center' style='BORDER-LEFT: 1px solid;BORDER-RIGHT: 1px solid;'>");
	    out.println("\n<I>Posted By:&nbsp;<B>"+fd.getUser()+"</B>&nbsp;"+fd.getDate()+"</I></TD></TR>");
	    out.println("\n<TR><TD align='left' style='BORDER-LEFT: 1px solid; BORDER-RIGHT: 1px solid;'>");
	    out.println("\n<P style='margin-left:2.5em; margin-right:2.5em; margin-top:1.0em; margin-bottom: 3.0em;'>");
	    out.println(fd.getText());
	    out.println("\n</P>");
	    out.println("\n</TD></TR>");
	    out.println("\n<TR><TD style='BORDER: 1px solid;'>"+msbf+"</TD></TR>");
	    out.println("\n</TABLE>");
	
	    //======================================================================================
	    // Link
	    //======================================================================================
	    
	    //if(f13 != null && f13.length()>1 && (f13.toLowerCase()).startsWith("http")) {
	    //    out.println("<TABLE cellSpacing=0 cellPadding=0 align='center'>");
	    //    out.println("<TR><TD align=center>The following page is a link from:</TD></TR>");
	    //    out.println("<TR><TD style='font-size:8pt'><input type=text size=80 value=\""+f13+"\"></TD></TR>");
	    //    out.println("</TABLE>");    
	    //    out.println("<TABLE cellSpacing=0 cellPadding=0 align='center'>");
	    //    out.println("<TR><TD align='center'>");
	    //    out.println("<IFRAME src="+f13+" width=780 height=800></IFRAME>");
	    //    out.println("</TD></TR>");
	    //    out.println("</TABLE>");
	    //}
	    
	    //======================================================================================
	    // Reply
	    //======================================================================================
	    /*
	    if(fd.allowReply()) {
		out.println("<BR>");
		try {
		    //request.setAttribute("errorCallBackName","aid");
		    //request.setAttribute("errorCallBackValue","2-1");
		    request.setAttribute("ParentForumDetails", fd);
		    RequestDispatcher rd = request.getRequestDispatcher("/ForumAddNewServlet");
		    rd.include(request,response);
		}
		catch (Exception e) {
		}
	    }
	    */
	    //======================================================================================
	    // set print detail
	    //======================================================================================
	    
	    StringBuffer dsbf = null;
	    dsbf = new StringBuffer();
	    dsbf.append("<HTML><HEAD><TITLE>3WCenter.com ["+fd.getTitle()+"]</TITLE>");
	    dsbf.append("<SCRIPT>");
	    dsbf.append("window.resizeTo(800,500);");
	    dsbf.append("</SCRIPT>");
	    dsbf.append("</HEAD><BODY>");
	    dsbf.append("<TABLE border=0 width=100%>");
	    dsbf.append("<TR><TD align=center><H3>"+fd.getTitle()+"</H3></TD></TR>");
	    dsbf.append("<TR><TD align=center>");
	    dsbf.append("<I>Posted By:&nbsp;<B>"+fd.getUser()+"</B>&nbsp;"+fd.getDate()+"</I></TD></TR>");
	    dsbf.append("<TR><TD align=center>&nbsp;</TD></TR>");
	    dsbf.append("</TABLE>");		
	    dsbf.append("<TABLE border=0 width=100%>");
	    dsbf.append("<TR><TD><img src=/images/space.gif width=50 border=0></TD>");
	    dsbf.append("<TD align=left>");
	    dsbf.append(fd.getText());
	    dsbf.append("</TD>");
	    dsbf.append("<TD><img src=/images/space.gif width=50 broder-0></TD>");
	    dsbf.append("</TR>");
	    dsbf.append("<TR><TD align=center colspan=3>");
	    dsbf.append("<BR><BR><img src='/images/copy_right.gif' border='0'>");
	    dsbf.append("</TD></TR>");
	    dsbf.append("</TABLE>");		
	    dsbf.append("</BODY></HTML>");
	    
	    //session.setAttribute("PrintBuffer",dsbf);

	    //==================================================
	    // update reader
	    //==================================================
	    
	    //ejbdb.update(query);
	}
	catch(IOException e) {
	    System.out.println("ForumServlet:Error:IOException:"+e);
	}
	catch(Exception e) { 
	    System.out.println("ForumServlet:Error:Exception:"+e);
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
	doPost(request, response);
    }
}






